package com.puyuan.service.impl;

import com.puyuan.dao.impl.StudentDaoImpl;
import com.puyuan.domain.PageBean;
import com.puyuan.domain.Student;
import com.puyuan.service.StudentService;

import java.util.List;


public class StudentServiceImpl implements StudentService {
    StudentDaoImpl studentDao = new StudentDaoImpl();
    @Override
    public int addStu(Student s) {
        return studentDao.addStu(s);
    }

    @Override
    public PageBean<Student> findStu(String cp, String ps,String name1,String gender1) {
        //业务处理【根据是否有条件进行页面分页需要的资源封装】
        PageBean<Student> pageBean = new PageBean<>();
        //需要查询pageBean需要的五个数据
        //当前页和页面容量 有可能没有传入【默认是第一页 页面容量根据自己的需求去定义】
        if (cp == null || "".equals(cp)){
            cp = "1";//设置定义为第一页
        }
        if (ps == null || "".equals(ps)){
            ps = "4";//设置定义每页只显示四条
        }
        //转变当前页和页面容量的数据类型【从int转到String】  匹配数据库的数据类型
        int currentPage = Integer.parseInt(cp);
        int pageSize = Integer.parseInt(ps);
        //进行分页数据内容的查询
        List<Student> list = studentDao.findStu(currentPage, pageSize,name1,gender1);
        //查询总条数
        int totalCount = studentDao.findStuCount(name1,gender1);
        //查询总页数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        //封装数据到pageBean中返回
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalPage(totalPage);
        pageBean.setTotalCount(totalCount);
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public int deleteStu(String ids) {
        return studentDao.deleteStu(ids);
    }

    @Override
    public Student findStuById(int id) {
        return studentDao.findStuById(id);
    }

    @Override
    public int modifyStu(Student s) {
        return studentDao.modifyStu(s);
    }
}

